import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore(
  'user',
  () => {
    const userInfo = ref<any>({}) // 用户信息
    const count = ref(0)
    const increment = () => {
      count.value++
    }
    const setUserInfo = (val: any) => {
      userInfo.value = val
    }
    const clearUserInfo = () => {
      userInfo.value = {}
    }
    return {
      userInfo,
      setUserInfo,
      clearUserInfo,
      count,
      increment,
    }
  },
  {
    persist: {
      storage: {
        setItem(key, value) {
          if (localStorage) {
            localStorage.setItem(key, value)
          } else {
            uni.setStorageSync(key, value)
          }
        },
        getItem(key) {
          if (localStorage) {
            return localStorage.getItem(key)
          } else {
            return uni.getStorageSync(key)
          }
        },
      },
    },
  },
)
